/*
 * Copyright (C) 2010 Moduad Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.androidpn.client;

import java.util.Random;

import cd.safe.school.activity.SchoolSMS_detail;
import cd.safe.school.bean.ChatTuisong;
import cd.safe.school.util.User;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Toast;

/**
 * This class is to notify the user of messages with NotificationManager.
 * 
 * @author Sehwan Noh (devnoh@gmail.com)
 */
public class Notifier {
	public static Activity stopactivity;

	private static final String LOGTAG = LogUtil.makeLogTag(Notifier.class);

	private static final Random random = new Random(System.currentTimeMillis());

	private Context context;

	private SharedPreferences sharedPrefs;

	private NotificationManager notificationManager;

	public Notifier(Context context) {
		this.context = context;
		this.sharedPrefs = context.getSharedPreferences(
				Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
		this.notificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
	}

	public void notify(String notificationId, String apiKey, String title,
			String message, String uri, String from, String packetId) {
		Log.d(LOGTAG, "notify()...");

		Log.d(LOGTAG, "notificationId=" + notificationId);
		Log.d(LOGTAG, "notificationApiKey=" + apiKey);
		Log.d(LOGTAG, "notificationTitle=" + title);
		Log.d(LOGTAG, "notificationMessage=" + message);
		Log.d(LOGTAG, "notificationUri=" + uri);

		if (isNotificationEnabled()) {
			// Show the toast
			if (isNotificationToastEnabled()) {
				Toast.makeText(context, message, Toast.LENGTH_LONG).show();
			}

			// Notification
			Notification notification = new Notification();
			notification.icon = getNotificationIcon();
			notification.defaults = Notification.DEFAULT_LIGHTS;
			if (isNotificationSoundEnabled()) {
				notification.defaults |= Notification.DEFAULT_SOUND;
			}
			if (isNotificationVibrateEnabled()) {
				notification.defaults |= Notification.DEFAULT_VIBRATE;
			}
			notification.flags |= Notification.FLAG_AUTO_CANCEL;
			notification.when = System.currentTimeMillis();
			notification.tickerText = message;
			// ----------------------------------------点击通知栏，跳转页面
			// Intent intent = new Intent(context,
			// NotificationDetailsActivity.class);
			Intent intent = new Intent(context, SchoolSMS_detail.class);

			if (SchoolSMS_detail.tag) {
				if ((User.userID + "sendid").equals(SchoolSMS_detail.member)) {

					intent = new Intent("safe_school");
					intent.putExtra("apiKey", apiKey);

					intent.putExtra("from", from);
					intent.putExtra("message", message);
					intent.putExtra("notificationId", notificationId);
					intent.putExtra("packetId", packetId);
					intent.putExtra("title", title);
					intent.putExtra("uri", uri);
					context.sendBroadcast(intent);
				} else {
					stopactivity.finish();
					intent.putExtra("apiKey", apiKey);
					intent.putExtra("from", from);
					intent.putExtra("message", message);
					intent.putExtra("notificationId", notificationId);
					intent.putExtra("packetId", packetId);
					intent.putExtra("title", title);
					intent.putExtra("uri", uri);
					PendingIntent contentIntent = PendingIntent.getActivity(
							context, random.nextInt(), intent,
							PendingIntent.FLAG_UPDATE_CURRENT);

					notification.setLatestEventInfo(context, title, message,
							contentIntent);
					notificationManager.notify(random.nextInt(), notification);

				}

			} else {
				intent.putExtra("apiKey", apiKey);

				intent.putExtra("from", from);
				intent.putExtra("message", message);
				intent.putExtra("notificationId", notificationId);
				intent.putExtra("packetId", packetId);
				intent.putExtra("title", title);
				intent.putExtra("uri", uri);

				PendingIntent contentIntent = PendingIntent.getActivity(
						context, random.nextInt(), intent,
						PendingIntent.FLAG_UPDATE_CURRENT);

				notification.setLatestEventInfo(context, title, message,
						contentIntent);
				notificationManager.notify(random.nextInt(), notification);

				// Intent clickIntent = new Intent(
				// Constants.ACTION_NOTIFICATION_CLICKED);
				// clickIntent.putExtra(Constants.NOTIFICATION_ID,
				// notificationId);
				// clickIntent.putExtra(Constants.NOTIFICATION_API_KEY, apiKey);
				// clickIntent.putExtra(Constants.NOTIFICATION_TITLE, title);
				// clickIntent.putExtra(Constants.NOTIFICATION_MESSAGE,
				// message);
				// clickIntent.putExtra(Constants.NOTIFICATION_URI, uri);
				// // positiveIntent.setData(Uri.parse((new StringBuilder(
				// //
				// "notif://notification.adroidpn.org/")).append(apiKey).append(
				// // "/").append(System.currentTimeMillis()).toString()));
				// PendingIntent clickPendingIntent =
				// PendingIntent.getBroadcast(
				// context, 0, clickIntent, 0);
				//
				// notification.setLatestEventInfo(context, title, message,
				// clickPendingIntent);
				//
				// Intent clearIntent = new Intent(
				// Constants.ACTION_NOTIFICATION_CLEARED);
				// clearIntent.putExtra(Constants.NOTIFICATION_ID,
				// notificationId);
				// clearIntent.putExtra(Constants.NOTIFICATION_API_KEY, apiKey);
				// // negativeIntent.setData(Uri.parse((new StringBuilder(
				// //
				// "notif://notification.adroidpn.org/")).append(apiKey).append(
				// // "/").append(System.currentTimeMillis()).toString()));
				// PendingIntent clearPendingIntent =
				// PendingIntent.getBroadcast(
				// context, 0, clearIntent, 0);
				// notification.deleteIntent = clearPendingIntent;
				//
				// notificationManager.notify(random.nextInt(), notification);
			}
		} else {
			Log.w(LOGTAG, "Notificaitons disabled.");
		}
	}

	private int getNotificationIcon() {
		return sharedPrefs.getInt(Constants.NOTIFICATION_ICON, 0);
	}

	private boolean isNotificationEnabled() {
		return sharedPrefs.getBoolean(Constants.SETTINGS_NOTIFICATION_ENABLED,
				true);
	}

	private boolean isNotificationSoundEnabled() {
		return sharedPrefs.getBoolean(Constants.SETTINGS_SOUND_ENABLED, true);
	}

	private boolean isNotificationVibrateEnabled() {
		return sharedPrefs.getBoolean(Constants.SETTINGS_VIBRATE_ENABLED, true);
	}

	private boolean isNotificationToastEnabled() {
		return sharedPrefs.getBoolean(Constants.SETTINGS_TOAST_ENABLED, false);
	}

}
